package com.qzimp.fujiancuo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qzimp.fujiancuo.entity.Userinfo;
import com.qzimp.fujiancuo.entity.UserinfoLog;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.UserinfoHaveLogsVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.mapper.UserinfoMapper;
import com.qzimp.fujiancuo.service.UserinfoLogService;
import com.qzimp.fujiancuo.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author 吴宗波
 * @since 2024-11-21
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
    @Autowired
    private  UserinfoMapper userinfoMapper;
    @Autowired
    private UserinfoLogService userinfoLogService;

    @Override
    public Userinfo findUserinfoByLoginName(String loginName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);

        return userinfoMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public Userinfo login(String loginName, String password) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);
        queryWrapper.eq("password",password);
        Userinfo userinfo= userinfoMapper.selectOneByQuery(queryWrapper);
        if(userinfo==null){
            throw  new ApiException(ResultCode.USER_LOGIN_NAME_NOT_EXIST);
        }
        //生成token

        final  String token= JWT.create()
                .setNotBefore(DateUtil.date())
                .setKey("abcdef".getBytes())
                .sign();
        userinfo.setToken(token);
        //更新token
        int ok= userinfoMapper.update(userinfo);
        if(ok<1){
            if(userinfo==null){
                throw  new ApiException(ResultCode.USER_TOKEN_UPDATE_FAILURE);
            }
        }

        return  userinfo;

    }

    @Override
    public UserinfoHaveLogsVo findUserinfoHaveLogsVo(String loginName) {
        //要通过userName用户的信息
        Userinfo userinfo=this.findUserinfoByLoginName(loginName);
        if(userinfo ==null){
            throw new ApiException(ResultCode.CUSTOM_ERROR,"该用户不存在");
        }
        //通过用户编号获取用户日志
        List<UserinfoLog> logs= userinfoLogService.findUserinfoLogsById(userinfo.getId());

        UserinfoHaveLogsVo userinfoHaveLogsVo=new UserinfoHaveLogsVo();
        userinfoHaveLogsVo.setLogs(logs);
        userinfoHaveLogsVo.setLoginName(loginName);
        return userinfoHaveLogsVo;
    }

    @Override
    public void updateUserinfo(Userinfo userinfo) {
        // 构建更新条件
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userinfo.getId());

        // 更新密码和盐值
        Userinfo updateInfo = new Userinfo();
        updateInfo.setPassword(userinfo.getPassword());
        updateInfo.setSalt(userinfo.getSalt());

        // 执行更新操作
        int updateCount = userinfoMapper.updateByQuery(updateInfo, queryWrapper);
        if (updateCount < 1) {
            throw new ApiException(ResultCode.USER_TOKEN_UPDATE_FAILURE);
        }
    }
}
